﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace N03_11HCA_HTTTHD.Controllers
{
    public class TimMuaController : Controller
    {
        Data_DAO_DTO_Lib.THONGTINTIMMUA_DAO tmDAO = new Data_DAO_DTO_Lib.THONGTINTIMMUA_DAO();

        Data_DAO_DTO_Lib.DANHSACHTIMMUAQUANTAM_DAO tmQTDAO = new Data_DAO_DTO_Lib.DANHSACHTIMMUAQUANTAM_DAO();
        [HttpPost]
        public JsonResult QuanTam(int id)
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.QuanLyKho);
            if (isCoQuyen == false)
            {
                return Json(new { Result = "ERROR", Message = "Không có quyền để thực hiện chức năng quan tâm tìm mua" });
            }

            try
            {
                Data_DAO_DTO_Lib.DTO.DANHSACHTIMMUAQUANTAM_DTO rvQT = new Data_DAO_DTO_Lib.DTO.DANHSACHTIMMUAQUANTAM_DTO();
                rvQT.IDTaiKhoanQuanTam = (int)N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session).ID1;
                rvQT.IDThongTinTimMua = id;
                tmQTDAO.Them(rvQT);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }

            return Json(new { Result = "OK" });
        }


        //
        // GET: /TimMua/
        public ActionResult Index()
        {
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);


            List<Data_DAO_DTO_Lib.DTO.DTO> list = tmDAO.LayDanhSachCoThamSo(0, TienIch.HeThong.SoLuongLoadTimMuaMacDinh, TienIch.HeThong.ChuoiSapXepTimMuaMacDinh);
            Models.TimMua modelTimMua = new Models.TimMua(list);

            return View("TimMua", modelTimMua);
        }

        [HttpPost]
        public JsonResult Index(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            List<Data_DAO_DTO_Lib.DTO.DTO> list = tmDAO.LayDanhSachCoThamSo(jtStartIndex, jtPageSize, jtSorting);

            return new JsonResult() { Data = list };
        }

        public ActionResult ChiTiet(int id_TimMua)
        {
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            Data_DAO_DTO_Lib.DTO.THONGTINTIMMUA_DTO tmDTO = (Data_DAO_DTO_Lib.DTO.THONGTINTIMMUA_DTO)tmDAO.LayTuID(id_TimMua);

            List<Data_DAO_DTO_Lib.DTO.DTO> listDanhMuc = tmDAO.LayDanhSachDanhMuc(tmDTO.ID);

            Models.TimMua_ChiTiet modelTimMua = new Models.TimMua_ChiTiet(tmDTO, listDanhMuc);

            return View("TimMua_ChiTiet", modelTimMua);
        }
    }
}
